package com.firegod.group;

import com.alibaba.fastjson.JSONObject;
import com.firegod.commonutils.vo.ResultVo;
import com.firegod.group.service.CommentService;
import com.firegod.group.service.TopicService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/group")
@Api(tags = "圈子管理")
public class TopicController {
    @Autowired
    private TopicService topicService;
    @Autowired
    private CommentService commentService;

    @PostMapping("/sendTopic")
    public ResultVo sendTopic(@RequestBody JSONObject jsonObject) {
        String groupId = jsonObject.getString("groupId");
        String topicTitle = jsonObject.getString("topicTitle");
        String topicDescribe = jsonObject.getString("topicDescribe");
        return ResultVo.ok().data("topicId", topicService.sendTopic(groupId, topicTitle, topicDescribe));
    }

    @GetMapping("/topics/{courseId}")
    public ResultVo getTopics(@PathVariable String courseId) {
        return ResultVo.ok().data("topicList", topicService.getTopicList(courseId));
    }

    @GetMapping("/topicInfo/{topicId}")
    public ResultVo getTopicInfo(@PathVariable String topicId) {
        System.out.println("帖子详情：id:" + topicId);
        return ResultVo.ok().data(topicService.getTopicInfo(topicId));
    }

    @PostMapping("/deleteTopic/{topicId}")
    public ResultVo deleteTopic(@NotNull @PathVariable String topicId) {
        topicService.deleteTopic(topicId);
        return ResultVo.ok();
    }

    @PostMapping("/updateTopic")
    public ResultVo updateTopic(@NotNull @RequestBody JSONObject jsonObject) {
        String topicId = jsonObject.getString("topicId");
        String title = jsonObject.getString("title");
        String content = jsonObject.getString("content");
        topicService.updateTopic(topicId, title, content);
        return ResultVo.ok();
    }

    @PostMapping("/likeTopic/{topicId}")
    public ResultVo likeTopic(@PathVariable @NotNull String topicId) {
        if (topicService.likeTopic(topicId)) {
            return ResultVo.ok();
        } else {
            return ResultVo.error();
        }
    }

    @PostMapping("/unLikeTopic/{topicId}")
    public ResultVo unLikeTopic(@PathVariable @NotNull String topicId) {
        if (topicService.unLikeTopic(topicId)) {
            return ResultVo.ok();
        } else {
            return ResultVo.error();
        }
    }

    @PostMapping("/featureTopic")
    public ResultVo featureTopic(@RequestBody JSONObject jsonObject) {
        String topicId = jsonObject.getString("topicId");
        Boolean feature = jsonObject.getBoolean("feature");
        topicService.featureTopic(topicId, feature);
        return ResultVo.ok();
    }

    @PostMapping("/changeTop")
    public ResultVo topTopic(@NotNull @RequestBody JSONObject jsonObject) {
        String topicId = jsonObject.getString("topicId");
        Boolean top = jsonObject.getBoolean("isTop");
        topicService.topTopic(topicId, top);
        return ResultVo.ok();
    }

    @GetMapping("/topTopic/{groupId}")
    public ResultVo topTopics(@PathVariable String groupId) {
        return ResultVo.ok().data("topTopics", topicService.getTopTopics(groupId));
    }
}
